package cn.tedu.fresheveryday.controller;

import cn.tedu.fresheveryday.pojo.entity.Product;
import cn.tedu.fresheveryday.pojo.entity.User;
import cn.tedu.fresheveryday.pojo.entity.ViewHistory;
import cn.tedu.fresheveryday.pojo.dto.ProductPagerDTO;
import cn.tedu.fresheveryday.pojo.vo.ProductDetailVO;
import cn.tedu.fresheveryday.pojo.vo.ProductSimpleVO;
import cn.tedu.fresheveryday.restful.JsonResult;
import cn.tedu.fresheveryday.service.IProductService;
import cn.tedu.fresheveryday.service.IViewHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@Api(tags = "商品模块")
@RequestMapping(path = "/product", produces = "application/json;charset=utf-8")
public class ProductController {
    @Autowired
    private IProductService service;

    @Autowired
    private IViewHistoryService historyService;

    /**
     * 通过id查询商品信息
     * @param id 商品id
     * @param session HttpSession对象
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation("通过id查询商品信息")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public ProductDetailVO selectById(Integer id, HttpSession session) {
        // 查询登录的用户信息
        User user = (User) session.getAttribute("user");

        log.debug("用户登录信息：{}", user);

        // 如果当前有用户登录
        if (user != null) {
            String userId = user.getId();
            // 通过商品id和用户id查询浏览历史
            List<ViewHistory> list = historyService.selectByUserIdAndProductId(userId, id);

            // 创建一个历史记录对象，用于接下来的操作
            ViewHistory history = new ViewHistory();

            // 如果浏览历史中没有该商品的信息，保存
            if (list.isEmpty()) {
                history.setId(UUID.randomUUID().toString()); // 设置随机id
                history.setProductId(id); // 设置商品id
                history.setUserId(userId); // 设置用户id
                history.setViewTime(LocalDateTime.now());

                // 保存浏览历史
                historyService.insert(history);
            } else {
                // 修改浏览记录的浏览时间为当前时间
                history = list.get(0);

                historyService.updateById(history.getId(), history.getViewTime());
            }
        }

        return service.selectById(id);
    }

    /**
     * 查询最新上架的商品
     * @return
     */
    @GetMapping("/selectNew")
    @ApiOperation("查询最新上架的3个商品")
    public List<ProductSimpleVO> selectNew() {
        return service.selectNew();
    }

    /**
     * 分页查询商品
     * @param productPagerDTO
     * @return
     */
    @ApiOperation("分页查询全部商品")
    @GetMapping("/selectByPage")
    public Map<String, Object> selectByPage(ProductPagerDTO productPagerDTO) {
        System.out.println(productPagerDTO);
        List<Product> products = service.selectByPage(productPagerDTO);
        int total = service.selectCountByPage(productPagerDTO);

        return JsonResult.returnResultMap(total, products);
    }

}